﻿using Model.EntityInfo;
using Sunny.UI;

namespace WinFormsSprayProcessSCADASystem.Components
{
    public partial class DialogRoleEdit : UIEditForm
    {
        private EditRoleData editRoleData = new EditRoleData();
        private List<RouteInfoModel> routes;
        private List<TreeNode> _nodes = new List<TreeNode>();
        public DialogRoleEdit()
        {
            InitializeComponent();
        }


        protected override bool CheckData()
        {
            if (string.IsNullOrEmpty(uiTextBox1.Text))
            {
                this.ShowWarningDialog("请输入角色名称");
                uiTextBox1.Focus();
                return false;
            }
            if (editRoleData.routeIds.Count == 0)
            {
                this.ShowWarningDialog("请选菜单权限");
                uiComboTreeView1.Focus();
                return false;
            }
            Console.WriteLine("校验通过");
            return true;
        }

        public EditRoleData EditRoleData
        {
            get
            {
                Console.WriteLine(editRoleData == null);
                if (editRoleData == null)
                {
                    editRoleData = new EditRoleData();
                }

                editRoleData.Name = uiTextBox1.Text;
                return editRoleData;
            }

            set
            {
                editRoleData = value;
                uiTextBox1.Text = value.Name;
                //if (value.routeIds.Count > 0 && uiComboTreeView1.Nodes.Count > 0)
                //{
                //    //uiComboTreeView1.Nodes.AddRange(BuildNodes(routes));
                //    //uiComboTreeView1.Nodes.
                //    foreach (var item in _nodes)
                //    {
                //        if (value.routeIds.Contains(Convert.ToInt32(item.Tag)))
                //        {
                //            uiComboTreeView1.SelectedNode = item;
                //        }
                //    }
                //}

            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            uiComboTreeView1.Nodes.Clear();
            uiComboTreeView1.Nodes.AddRange(BuildNodes(routes));

            SetDefaultCheckedNodes(uiComboTreeView1.Nodes);
            //uiComboTreeView1.TreeView.Refresh();
            //uiComboTreeView1.TreeView.Update();

            //uiComboTreeView1.TreeView.
        }


        public List<RouteInfoModel> Routes
        {
            set
            {
                routes = value;
            }
        }


        private void uiSymbolLabel1_Click(object sender, EventArgs e)
        {

        }

        private TreeNode[] BuildNodes(List<RouteInfoModel> routes)
        {
            TreeNode[] res = new TreeNode[routes.Count];

            for (int i = 0; i < routes.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = routes[i].Name;
                node.Name = routes[i].Name;
                node.Tag = routes[i].Id;
                if (editRoleData.routeIds.Count > 0)
                {
                    Console.WriteLine(node.Text);
                    Console.WriteLine(routes[i].Id);
                    Console.WriteLine(editRoleData.routeIds.Contains(routes[i].Id));
                    node.Checked = editRoleData.routeIds.Contains(routes[i].Id);
                }
                res[i] = node;
                if (routes[i].Children != null && routes[i].Children.Count > 0)
                {
                    res[i].Nodes.AddRange(BuildNodes(routes[i].Children));
                }
                _nodes.Add(node);
            }

            return res;
        }

        private void SetDefaultCheckedNodes(TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                // 条件：节点文本包含指定关键字则选中
                if (editRoleData.routeIds.Contains(Convert.ToInt32(node.Tag)))
                {
                    node.Checked = true;
                }
                // 递归处理子节点
                if (node.Nodes.Count > 0)
                {
                    SetDefaultCheckedNodes(node.Nodes);
                }
            }
        }

        private void uiComboTreeView1_NodesSelected(object sender, TreeNodeCollection nodes)
        {
            editRoleData.routeIds.Clear();
            foreach (TreeNode item in nodes)
            {
                editRoleData.routeIds.Add(Convert.ToInt32(item.Tag));
            }
        }


    }

    public class EditRoleData
    {
        public int Id;
        public string Name;
        public List<int> routeIds = new List<int>();
    }
}
